package com.stars.home.vmodel

import android.app.Activity
import android.app.Application
import android.text.Editable
import android.text.TextUtils
import android.util.Log
import androidx.databinding.Bindable
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.hwangjr.rxbus.RxBus
import com.stars.home.BR
import com.stars.home.activity.ApplyActivity
import com.stars.home.activity.SurplusActivity
import com.stars.home.config.ConfigRx
import com.stars.home.utils.DecimalUtils
import com.stars.home.utils.net.NetEngine
import com.stars.home.view.SimpleTextWatcher
import com.trailer.base.base.BRepository
import com.trailer.base.base.BViewModel
import com.trailer.base.utils.UiSwitch
import kotlinx.coroutines.launch

class ApplyVModel(application: Application) : BViewModel<BRepository>(application) {

    var isRead= MutableLiveData<Int>(0)//0未勾选 1已勾选


    var type:String="";//1微信 2支付宝 3银行卡

    var money=MutableLiveData<String>()

   var  isShow=MutableLiveData<Boolean>(false)

    var bankId:String?=null

    var rate:Double=1.00

    var rateMoney=MutableLiveData<String>("0.00")



    @Bindable
    var code: String? = null
        set(value) {
            field = value
            notifyPropertyChanged(BR.code)
        }


    var oldText = ""
    var textWatcher = object : SimpleTextWatcher() {
        override fun afterTextChanged(s: Editable?) {
            super.afterTextChanged(s)
            var content = s.toString()
            if (!TextUtils.isEmpty(oldText) && content == oldText) {
                return
            }
            oldText = content
            //算利率
            setNumMoney(content)


        }

    }

    fun setNumMoney(content:String?){

        if(content.equals("null")||null==content|| content == ""|| content == " "){
            rateMoney.value= "0.00"
            return
        }
        
       var money= content!!.toDouble()*rate
        rateMoney.value= DecimalUtils.getNoCarry(money)
    }


    fun toAll(){
        code=money.value
    }

    fun toSubmit(){
        if(code.isNullOrEmpty()){
            toast("请输入提现金额")
            return
        }
        if(type.isNullOrEmpty()){
            toast("请输入提现方式")
            return
        }
        if(isRead.value==0){
            toast("请勾选")
            return
        }
        if(type == "3"){
            viewModelScope.launch {
                var it=repository().lreq { NetEngine.service.withdrawal(code,bankId) }

                if(it.ok(true)){
                    //提现成功
                    RxBus.get().post(ConfigRx.REFRESH_USER, "")


                    (repository().context as Activity).finish()
                }


            }


        }

    }

    fun getRate(){
        viewModelScope.launch {
            var it=repository().lreq { NetEngine.service.configList() }
            if(it.ok()){

                if(null!=it.data?.technicianWithdrawalFee) {
                    rate = it.data?.technicianWithdrawalFee!!.toDouble()
                }
            }
        }
    }



}
